package housing.task2.serializator;

import housing.task1.actor.Address;
import housing.task1.actor.Dispatcher;
import housing.task1.actor.Renter;
import housing.task1.actor.Worker;
import housing.task1.document.Request;
import housing.task1.document.WorkPlan;

import java.io.File;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 30.11.2010
 * Time: 17:46:32
 */
public abstract class AbstractWriter {
    private File rentersOut;
    private File addressOut;
    private File workerOut;
    private File dispatcherOut;
    private File requestOut;
    private File workPlanOut;


    public void Writer() {
        setDefaultOutput();
    }


    @Deprecated
    public abstract void write(Renter renter);

    @Deprecated
    public abstract void write(Address address);

    @Deprecated
    public abstract void write(Worker worker);

    @Deprecated
    public abstract void write(Dispatcher dispatcher);

    @Deprecated
    public abstract void write(Request request);

    @Deprecated
    public abstract void write(WorkPlan workPlan);

    protected abstract void setDefaultOutput();

    public void erase(File file) {
        String name = file.getName();
        if (file.delete()) {
            System.out.println("File " + name + " deleted");
        } else {
            System.out.println("Problems deleting file " + name);
        }
    }

    protected void checkFile(File file) {
        if (!file.exists()) {
            String dirPath = file.getParent();
            new File(dirPath).mkdir();
            try {
                file.createNewFile();
                //logger.addInfo(file created);
                System.out.println("File " + file.getAbsolutePath() + " created");
            } catch (IOException e) {
                e.printStackTrace();
                //logger failed create a new file
            }
        }
    }


    //**********************  Getters and Setters *****************************************************

    /**
     * Each setter check outputFile first and creates it, if it doesn't exist
     *
     * @param rentersOut
     */

    public void setRentersOut(File rentersOut) {
        checkFile(rentersOut);
        this.rentersOut = rentersOut;
    }

    public void setAddressOut(File addressOut) {
        checkFile(addressOut);
        this.addressOut = addressOut;
    }

    public void setWorkerOut(File workerOut) {
        checkFile(workerOut);
        this.workerOut = workerOut;
    }

    public void setDispatcherOut(File dispatcherOut) {
        checkFile(dispatcherOut);
        this.dispatcherOut = dispatcherOut;
    }

    public void setRequestOut(File requestOut) {
        checkFile(requestOut);
        this.requestOut = requestOut;
    }

    public void setWorkPlanOut(File workPlanOut) {
        checkFile(workPlanOut);
        this.workPlanOut = workPlanOut;
    }


    public File getRentersOut() {
        return rentersOut;
    }

    public File getAddressOut() {
        return addressOut;
    }

    public File getWorkerOut() {
        return workerOut;
    }

    public File getDispatcherOut() {
        return dispatcherOut;
    }

    public File getRequestOut() {
        return requestOut;
    }

    public File getWorkPlanOut() {
        return workPlanOut;
    }
}//interface AbstractWriter
